<?php
namespace app\spadmin\controller\module;
use app\common\controller\Backend;
use think\Db;
class Goods extends Backend{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Goods');
    }

    public function index(){
        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['title'] = ['like', '%' . $data['keywords'] . '%'];
            }
            if (isset($data['type']) && $data['type']) {
                $map['type'] = $data['type'];
            }else{
                $map['type'] = ['in','2,3'];
            }
            if (isset($data['attr']) && $data['attr']) {
                switch ($data['attr']) {
                    case 'sale':
                        $map['is_sale'] = 1;
                        break;
                    case 'score':
                        $map['is_score'] = 1;
                        break;
                    case 'discount':
                        $map['is_discount'] = 1;
                        break;
                    default:
                        break;
                }
            }
            if (isset($data['commend']) && $data['commend']) {
                switch ($data['commend']) {
                    case 'index':
                        $map['is_index'] = 1;
                        break;
                    case 'hot':
                        $map['is_hot'] = 1;
                        break;
                    case 'new':
                        $map['is_new'] = 1;
                        break;
                    default:
                        break;
                }
            }
            if (isset($data['status']) && $data['status']) {
                $map['status'] = $data['status'];
            } else {
                $map['status'] = 1;
            }
            $list =   $this->model->where($map)->order('sort desc')->paginate($data['limit'])->toArray();
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    public function edit($ids=null){
        $row = $this->model->get($ids);
        if($this->request->isPost()){
            if($row == null){
                $row = $this->model;
            }
            $data = $this->request->post('',[],'strip_tags');
            if(empty($data['thumb'])){
                $this->error('请上传缩略图');
            }
            $data['content'] = $this->request->post('content','');
            $data['images'] = $data['thumb'];
            $data['is_hot'] = 0;
            $data['is_index'] = 0;
            $data['is_new'] = 0;
            $data['is_all'] = $data['is_all']??0;
            $data['is_well'] = $data['is_well']??0;
            $data['is_gift'] = $data['is_gift']??0;
            $data['is_office'] = $data['is_office']??0;
            $data['is_sale'] = $data['is_sale']??0;
            $data['is_discount'] = $data['is_discount']??0;
            $data['is_score'] = $data['is_score']??0;
            $result = $row->allowField(true)->save($data,$data['id']?['id'=>$data['id']]:null);
            if($result === false){
                $this->error($row->getError());
            }
            $this->success('操作成功');
        }
        $row['images']=$row['images']?explode(',',$row['images']):[];
        $row['args'] = $row['args']?json_decode($row['args'],1):[];
        $row['sku'] = $row['sku']?json_decode($row['sku'],1):[];
        $this->assign('info',$row);
        return $this->view->fetch();

    }
    public function site($ids=null){
        
        if($this->request->isPost()){
            $data = $this->request->post('',[],'strip_tags');
            $row = $this->model->get($data['id']);
            if(empty($data['store_id'])){
                $this->error('请选择服务商');
            }
             Db::startTrans();
            try {
                model('GoodsStore')->where('goods_id',$row['id'])->delete();
                $store = explode( ',',$data['store_id']);
                $insert = [];
                foreach ($store as $key => $value) {
                    $insert[] = [
                        'goods_id'=>$row['id'],
                        'store_id'=>$value
                    ];
                }
                model('GoodsStore')->saveAll($insert);
                Db::commit();
                $this->success('操作成功');
            } catch (\think\exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            
        }else{
            $row = $this->model->get($ids);
            $store = model('Store')->where('status',1)->where('type',1)->where('property',1)->order('id desc')->column('id,company_name,id,mobile,nickname');
            $store_id = model('GoodsStore')->where('goods_id',$row['id'])->column('store_id');
            $this->assign('store',$store);
            $this->assign('store_id',$store_id);
            $this->assign('info',$row);
            return $this->view->fetch();
        }
    }
}